<template>
  <div>
    <basic-modal :modalOpt="modal" :footer="null" @closeModal="closeModal">
      <template>
        <basic-form
          :formInfos="s_FormInfo"
          :formDatas="s_formDatas"
          :rules="s_rules"
          @onSubmit="onSubmit"
          @onChangeType="onChangeType"
          @closeModal="closeModal"
        ></basic-form>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import basicForm from '../common/basicForm'
import { getLicenseByPid } from '@/views/app/biz/regisLicense/regisLicense'
import { getInfoByPid, saveInfo } from './PlatformData'

const s_FormInfo = [
  { label: '数据有效区域(国、省、市、县、院)', name: 'areaUse',  type: 'input' , placeholder: '请输入' },
  { label: '商品自编码', name: 'code', type: 'input' , placeholder: '请输入' },
  { label: '品名', name: 'productNameZh', type: 'input' , placeholder: '请输入' },
  { label: '英文名称', name: 'productNameEn', type: 'input' , placeholder: '请输入' },
  { label: '通用名(商品名)', name: 'commonName', type: 'input' , placeholder: '请输入' },
  { label: '一级分类（学科、品类）', name: 'first', type: 'input' , placeholder: '请输入' },
  { label: '二级分类（用途、品目）', name: 'second', type: 'input' , placeholder: '请输入' },
  { label: '三级分类（部位、功能、品种）', name: 'third', type: 'input' , placeholder: '请输入' },
  { label: '分类', name: ['first','second','third'], type: 'threeLevelSelect' , placeholder: '请选择分类' },
  { label: '规格（特征、参数）', name: 'spec', type: 'input' , placeholder: '请输入' },
  { label: '型号(剂型)', name: 'model', type: 'input' , placeholder: '请输入' },
  { label: '医保代码', name: 'medicareCode', type: 'input' , placeholder: '请输入' },
  { label: '描述', name: 'disc', type: 'input' , placeholder: '请输入' },
  { label: '品牌', name: 'brand', type: 'input' , placeholder: '请输入' },
  { label: '上市许可持有企业社会编码', name: 'mahCode', type: 'input' , placeholder: '请输入' },
  { label: '上市许可持有企业中文名', name: 'mahZh', type: 'input' , placeholder: '请输入' },
  { label: '上市许可持有企业英文名', name: 'mahEn', type: 'input' , placeholder: '请输入' },
  { label: '生产企业社会编码', name: 'manufacturerCode', type: 'input' , placeholder: '请输入' },
  { label: '生产企业中文名称', name: 'manufacturerZh', type: 'input' , placeholder: '请输入' },
  { label: '生产企业英文名称', name: 'manufacturerEn', type: 'input' , placeholder: '请输入' },
  { label: '产地', name: 'origin', type: 'input' , placeholder: '请输入' },
  { label: '产品类别', name: 'category', type: 'dicselect',dicName: 'medicalCategory', placeholder: '请选择产品类别' },
  { label: '计量单位', name: 'measurement', type: 'input' , placeholder: '请输入' },
  { label: '药品本位码', name: 'sdc', type: 'input' , placeholder: '请输入' },
  { label: '当前批准类型', name: 'approvalType',type: 'dicselect',dicName: 'medicalApprovalType', placeholder: '请选择当前批准类型' },
  { label: '当前注册证号/注册证号', name: 'approvalNum', type: 'zchselect', zhuangTai:'正常', placeholder: '请输入' },
  { label: ['有效期','到'], name: ['approvalDate','validDate'], type: 'datatimeR', placeholder: '请输入' },
  { label: '状态', name: 'state',type: 'dicselect',dicName: 'medicalProdState', placeholder: '请选择产品状态' },
  { label: '结算方式', name: 'settlement', type: 'input' , placeholder: '请输入' },
  { label: '阳采编码', name: 'acquisitionCode', type: 'input' , placeholder: '请输入' },
  { label: ['保存', '重置'], type: 'botton' }
]
const s_formDatas = {
  sysSeq: 0,
  pid: '',
  code: null,
  productNameZh: null,
  productNameEn: null,
  commonName: null,
  class:['f11','s22','t22'],
  first: 'f11',
  second: 's22',
  third: 't11',
  spec: null,
  model: null,
  medicareCode: null,
  disc: null,
  brand: null,
  mahCode: null,
  mahZh: null,
  mahEn: null,
  manufacturerCode: null,
  manufacturerZh: null,
  manufacturerEn: null,
  origin: null,
  category: null,
  measurement: null,
  sdc: null,
  approvalType: null,
  approvalNum: null,
  approvalDate: null,
  validDate: null,
  state: null,
  settlement: null,
  acquisitionCode: null,
}
const s_rules = {
  productNameZh: [{ required: true, message: '品名必填', trigger: 'blur' }],
  spec: [{ required: true, message: '规格必填，没有请填“无”', trigger: 'blur' }],
  code: [{ required: true, message: '产品自编码必填', trigger: 'change' }],
  first: [{ required: true, message: '一级分类必填', trigger: 'change' }],
  second: [{ required: true, message: '二级分类必填', trigger: 'change' }],
  third: [{ required: true, message: '三级分类必填', trigger: 'change' }],
  validDate: [{ required: true, message: '有效期止必填', trigger: 'change' }],

}
export default {
  components: {
    basicModal,
    basicForm
  },
  data() {
    return {
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '新增商品',
        modalWidth: '1000px'
      },
      s_FormInfo: s_FormInfo,
      s_formDatas: s_formDatas,
      s_rules: s_rules
    }
  },
  computed: {},
  mounted() {},
  methods: {
    //自动生成FormData
    autoFormData() {
      let str = ''
      for (const item of s_FormInfo) {
        str += item.name + ': null,'
      }
      console.log(str)
    },
    // 新增按钮 点击了新增
    addModal() {
      this.autoFormData()
      console.log('点击了新增')
      this.s_formDatas = {
        sysSeq: 0,
        pid: '',
        code: null,
        productNameZh: null,
        productNameEn: null,
        commonName: null,
        class:['f11','s22','t22'],
        first: 'f11',
        second: 's22',
        third: null,
        spec: null,
        model: null,
        medicareCode: null,
        disc: null,
        brand: null,
        mahCode: null,
        mahZh: null,
        mahEn: null,
        manufacturerCode: null,
        manufacturerZh: null,
        manufacturerEn: null,
        origin: null,
        category: null,
        measurement: null,
        sdc: null,
        approvalType: null,
        approvalNum: null,
        approvalDate: null,
        validDate: null,
        state: null,
        settlement: null,
        acquisitionCode: null,
      }
      this.modal.modalVisible = true
      this.modal.modalTitle = '新增商品'
    },
    // 编辑按钮 
    editModal(val) {
      console.log('点击了编辑')
      this.getSBypid(val)
      this.modal.modalVisible = true
      this.modal.modalTitle = '修改商品'
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    // 保存成功关闭模态详细框
    closeModal2() {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    // 模态详细框提交按钮
    onSubmit(val) {
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', duration: 2 })
        this.closeModal2()
      } else {
        this.$message.error({ content: '提交失败', duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          this.s_formDatas = res.data
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 表单编号
    onChangeType(name, val) {
      console.log('formName', name)
      console.log('val', val)
      switch (name) {
        case 'zhuCezhPid':
          // console.log('aaaaaaaaaaaaaa',name)
          const zhucez = this.getZhuCezxx(val)
          break
      }
    },
    // 获取注册证详细信息
    async getZhuCezxx(pid) {
      const res = await getLicenseByPid(pid)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.s_formDatas = Object.assign(this.s_formDatas, { zhuCezhPid: res.data.pid })
        this.s_formDatas = Object.assign(this.s_formDatas, { zhuCezh: res.data.zhengZhaohm })
        this.s_formDatas = Object.assign(this.s_formDatas, { shengChanqymc: res.data.shengChancsmc })
        this.s_formDatas = Object.assign(this.s_formDatas, { pinMing: res.data.chanPinmc })
      }
    },
    //
    restData(val) {
      this.getSBypid(this.s_formDatas.pid)
    }
  }
}
</script>

<style></style>
